import Ux from 'ux';
import {Modal} from 'antd';

const rxConnect = (reference) => (event) => {
    Ux.prevent(event);
    const {$activeKey, $regions = []} = reference.state;
    const region = Ux.elementUnique($regions, 'key', $activeKey);
    if (region) {
        const buttonId = `action-${region.value}`;
        Ux.connectId(buttonId);
    }
}
const runActive = (reference, $activeKey) => {
    const state = {};
    state.$activeKey = $activeKey;
    state.$refresh = false;
    reference.setState(state);
}
const rxTabClick = (reference) => ($activeKey) => {
    const {$refresh = false} = reference.state;
    if ($refresh) {
        // 未保存
        const config = Ux.fromHoc(reference, "window");
        config.onOk = (event) => {
            Ux.prevent(event);
            runActive(reference, $activeKey);
        }
        Modal.confirm(config)
    } else {
        // 直接处理
        runActive(reference, $activeKey);
    }
}
export default {
    rxConnect,
    rxTabClick,
}